﻿using DotNetCore.CAP;
using Windows.Assistance.Domain;
using Windows.Assistance.Infrastructure.EFCore;
using Windows.Event.Share;

namespace Windows.Assistance.Application.Handlers
{
    public class LogSignInAddEventHandler : IEventHandler<LogSignInAddEvent>
    {
        private readonly AssistanceDbContext _db;

        public LogSignInAddEventHandler(AssistanceDbContext db)
        {
            _db = db;
        }
        [CapSubscribe(nameof(LogSignInAddEvent))]
        public async Task Handler(LogSignInAddEvent eventData)
        {
            LogSignIn model = new()
            {
                UserAgent = eventData.UserAgent,
                Browser = eventData.Browser,
                Os = eventData.Os,
                IP = eventData.IP,
                City = eventData.City,
                UserName = eventData.UserName,
                IsSuccess = eventData.IsSuccess,
            };
            await _db.AddAsync(model);
            await _db.SaveChangesAsync();
        }
    }
}
